
#include <QDebug>
#include <QPainter>
#include <QStringList>
#include <QString>
#include <QFile>
#include <QTextStream>
#include "arena.h"
#include "losttankwindow.h"



Arena::Arena()
{
    grid=new Grid;
}


void Arena::setBackground(const QPixmap& pixmap)
{

    this->setBackgroundBrush(QBrush(pixmap));
    setSceneRect(-pixmap.width()/2,-pixmap.height()/2,pixmap.width(),pixmap.height());

}

void Arena::buildGrid()
{
    grid->build(width(),height());
    for(int i=0;i<grid->height();++i)
        for(int j=0;j<grid->width();++j)
        {
            QGraphicsItem* rect=addRect(grid->tiles[i][j]->x-width()/2,grid->tiles[i][j]->y-height()/2,100,100);
            rect->hide();
            rects.push_back(rect);
        }

}


void Arena::addPurgatory(Purgatory* p)
{
    purgatory=p;
}

void Arena::addPlayer(Player* player,int i, int j)
{

    grid->player=player;
    player->setPos(j*Tile::TILE_SIZE-width()/2+Tile::TILE_SIZE/2,i*Tile::TILE_SIZE-height()/2+Tile::TILE_SIZE/2);
    player->grid=grid;
    player->tile=grid->tiles[i][j];
    player->purgatory=purgatory;
    player->init();
    player->arena=this;
    addItem(player);
}

void Arena::addPackage(Package* p,int i, int j)
{
    grid->objects[ot::PACKAGE].push_back(p);
    p->setPos(j*Tile::TILE_SIZE-width()/2+Tile::TILE_SIZE/2,i*Tile::TILE_SIZE-height()/2+Tile::TILE_SIZE/2);
    p->grid=grid;
    p->tile=grid->tiles[i][j];
    p->purgatory=purgatory;
    if(p->scene()==NULL)addItem(p);

}

void Arena::addWall(Wall* w,int i,int j,bool horizontal)
{
    grid->objects[ot::WALL].push_back(w);
    w->setPos(j*Tile::TILE_SIZE-width()/2+Tile::TILE_SIZE/2,i*Tile::TILE_SIZE-height()/2+Tile::TILE_SIZE/2);
    if(horizontal)w->rotate(90);
    w->grid=grid;
    w->tile=grid->tiles[i][j];
    w->tile->state=WALL;
    w->purgatory=purgatory;
    addItem(w);
}

void Arena::addObjective(Objective* o,int i,int j)
{
    grid->objects[ot::OBJECTIVE].push_back(o);
    o->setPos(j*Tile::TILE_SIZE-width()/2+Tile::TILE_SIZE/2,i*Tile::TILE_SIZE-height()/2+Tile::TILE_SIZE/2);
    o->grid=grid;
    o->tile=grid->tiles[i][j];
    o->tile->state=WALL;
    o->purgatory=purgatory;
    o->arena=this;
    addItem(o);
}

void Arena::addBullet(Bullet* o)
{
    grid->objects[ot::BULLET].push_back(o);
    addItem(o);
}

void Arena::buildLevel(int level)
{
    if(level==1)
    {

        QFile file(":/levels/level1.txt");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;

        QTextStream stream ( &file );
        QStringList list;

        int n=stream.readLine().split(":")[0].toInt();
        for(int i=0;i<n;++i)
        {
            list=stream.readLine().split(",");
            for(int j=list[0].toInt();j<=list[2].toInt();++j)
                addWall(new Wall,list[1].toInt(),j,true);
        }
        n=stream.readLine().split(":")[0].toInt();
        for(int i=0;i<n;++i)
        {
            list=stream.readLine().split(",");
            for(int k=list[1].toInt();k<=list[3].toInt();++k)
                addWall(new Wall,k,list[0].toInt(),false);
        }
        n=stream.readLine().split(":")[0].toInt();
        for(int i=0;i<n;++i)
        {
            list=stream.readLine().split(",");
            addObjective(new Objective,list[1].toInt(),list[0].toInt());

        }

        file.close();

        n=10;
        srand(time(0));
        if(!grid->objects[ot::PACKAGE].empty())
            n-=grid->objects[ot::PACKAGE].size();

        for(int i=0;i<n;++i)
        {
            int x,y;

            do
            {
                x=rand()%grid->width();
                y=rand()%grid->height();
            }
            while(grid->tiles[y][x]->state!=EMPTY);
            pt::PackageType t;
            int val;
            if(i%2)
            {
               t=pt::HEALTH;
               val=100;
            }
            else
            {
                t=pt::SHIELD;
                val=1;
            }
            ObjectFactory f(purgatory);
            addPackage(f.createPackage(t,val),y,x);
        }


        return;
    }

}
